<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - queue_kernel_2.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2004  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_QUEUE_KERNEl_2_
<font color='#0000FF'>#define</font> DLIB_QUEUE_KERNEl_2_

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='queue_kernel_abstract.h.html'>queue_kernel_abstract.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../assert.h.html'>../assert.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../interfaces/enumerable.h.html'>../interfaces/enumerable.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../interfaces/remover.h.html'>../interfaces/remover.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../serialize.h.html'>../serialize.h</a>"

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager <font color='#5555FF'>=</font> default_memory_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>class</font> <b><a name='queue_kernel_2'></a>queue_kernel_2</b> : <font color='#0000FF'>public</font> enumerable<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font>,
                           <font color='#0000FF'>public</font> remover<font color='#5555FF'>&lt;</font>T<font color='#5555FF'>&gt;</font>
    <b>{</b>

        <font color='#009900'>/*!
            REQUIREMENTS ON block_size
                0 &lt; block_size &lt; 2000000000

            INITIAL VALUE
                queue_size == 0   
                current_element == 0
                at_start_ == true
            
            CONVENTION
                queue_size == the number of elements in the queue
                at_start() == at_start_
                current_element_valid() == (current_element != 0)
                if (current_element_valid()) then
                    element() == current_element-&gt;item[current_element_pos]

                if (queue_size &gt; 0)                
                {                    
                    in-&gt;item[in_pos] == the spot where we will put the next item added
                                        into the queue
                    out-&gt;item[out_pos] == current()

                    when enqueuing elements inside each node item[0] is filled first, then 
                    item[1], then item[2], etc.
                                                         

                    each node points to the node inserted after it except for the most 
                    recently inserted node.  
                }
                
        !*/</font>


        <font color='#0000FF'>struct</font> <b><a name='node'></a>node</b>
        <b>{</b>
            node<font color='#5555FF'>*</font> next;

            T item[block_size];
        <b>}</b>;

        
        <font color='#0000FF'>public</font>:

            <font color='#0000FF'>typedef</font> T type;
            <font color='#0000FF'>typedef</font> mem_manager mem_manager_type;

            <b><a name='queue_kernel_2'></a>queue_kernel_2</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> :
                in<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                out<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                queue_size<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                current_element<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                at_start_<font face='Lucida Console'>(</font><font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <b>{</b>
            <b>}</b>

            <font color='#0000FF'>virtual</font> ~<b><a name='queue_kernel_2'></a>queue_kernel_2</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font>; 

            <font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='clear'></a>clear</b><font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font>;

            <font color='#0000FF'><u>void</u></font> <b><a name='enqueue'></a>enqueue</b> <font face='Lucida Console'>(</font>
                T<font color='#5555FF'>&amp;</font> item
            <font face='Lucida Console'>)</font>;

            <font color='#0000FF'><u>void</u></font> <b><a name='dequeue'></a>dequeue</b> <font face='Lucida Console'>(</font>
                T<font color='#5555FF'>&amp;</font> item
            <font face='Lucida Console'>)</font>;

            <font color='#0000FF'><u>void</u></font> <b><a name='cat'></a>cat</b> <font face='Lucida Console'>(</font>
                queue_kernel_2<font color='#5555FF'>&amp;</font> item
            <font face='Lucida Console'>)</font>;

            T<font color='#5555FF'>&amp;</font> <b><a name='current'></a>current</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font>;

            <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> <b><a name='current'></a>current</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;            

            <font color='#0000FF'><u>void</u></font> <b><a name='swap'></a>swap</b> <font face='Lucida Console'>(</font>
                queue_kernel_2<font color='#5555FF'>&amp;</font> item
            <font face='Lucida Console'>)</font>;

            <font color='#009900'>// functions from the remover interface
</font>            <font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='remove_any'></a>remove_any</b> <font face='Lucida Console'>(</font>
                T<font color='#5555FF'>&amp;</font> item
            <font face='Lucida Console'>)</font>;

            <font color='#009900'>// functions from the enumerable interface
</font>            <font color='#0000FF'>inline</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='size'></a>size</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

            <font color='#0000FF'>inline</font> <font color='#0000FF'><u>bool</u></font> <b><a name='at_start'></a>at_start</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

            <font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='reset'></a>reset</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

            <font color='#0000FF'><u>bool</u></font> <b><a name='current_element_valid'></a>current_element_valid</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

            <font color='#0000FF'>inline</font> <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> <b><a name='element'></a>element</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

            <font color='#0000FF'>inline</font> T<font color='#5555FF'>&amp;</font> <b><a name='element'></a>element</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font>;

            <font color='#0000FF'><u>bool</u></font> <b><a name='move_next'></a>move_next</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>;

        <font color='#0000FF'>private</font>:

            <font color='#0000FF'><u>void</u></font> <b><a name='delete_nodes'></a>delete_nodes</b> <font face='Lucida Console'>(</font>
                node<font color='#5555FF'>*</font> start,
                node<font color='#5555FF'>*</font> end
            <font face='Lucida Console'>)</font>;
            <font color='#009900'>/*!
                requires
                    - start points to a node in a singly linked list 
                    - start-&gt;next points to the next node in the list 
                    - by following the next pointers you eventually hit the node pointed
                      to by end
                ensures
                    - calls delete on the start node, the end node, and all nodes in between
            !*/</font>

            <font color='#009900'>// data members
</font>
            <font color='#0000FF'>typename</font> mem_manager::<font color='#0000FF'>template</font> rebind<font color='#5555FF'>&lt;</font>node<font color='#5555FF'>&gt;</font>::other pool; 

            node<font color='#5555FF'>*</font> in;
            node<font color='#5555FF'>*</font> out;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> queue_size;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> in_pos;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> out_pos;


            <font color='#0000FF'>mutable</font> node<font color='#5555FF'>*</font> current_element;
            <font color='#0000FF'>mutable</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> current_element_pos;
            <font color='#0000FF'>mutable</font> <font color='#0000FF'><u>bool</u></font> at_start_;

            <font color='#009900'>// restricted functions
</font>            <b><a name='queue_kernel_2'></a>queue_kernel_2</b><font face='Lucida Console'>(</font>queue_kernel_2<font color='#5555FF'>&amp;</font><font face='Lucida Console'>)</font>;        <font color='#009900'>// copy constructor
</font>            queue_kernel_2<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>=</font><font face='Lucida Console'>(</font>queue_kernel_2<font color='#5555FF'>&amp;</font><font face='Lucida Console'>)</font>;    <font color='#009900'>// assignment operator
</font>
    <b>}</b>;

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='swap'></a>swap</b> <font face='Lucida Console'>(</font>
        queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> a, 
        queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> b 
    <font face='Lucida Console'>)</font> <b>{</b> a.<font color='#BB00BB'>swap</font><font face='Lucida Console'>(</font>b<font face='Lucida Console'>)</font>; <b>}</b> 

    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='deserialize'></a>deserialize</b> <font face='Lucida Console'>(</font>
        queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> item, 
        std::istream<font color='#5555FF'>&amp;</font> in
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>try</font>
        <b>{</b>
            item.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> size;
            <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>size,in<font face='Lucida Console'>)</font>;
            T temp;
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> size; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>temp,in<font face='Lucida Console'>)</font>;
                item.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>
        <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>serialization_error e<font face='Lucida Console'>)</font>
        <b>{</b> 
            item.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>throw</font> <font color='#BB00BB'>serialization_error</font><font face='Lucida Console'>(</font>e.info <font color='#5555FF'>+</font> "<font color='#CC0000'>\n   while deserializing object of type queue_kernel_2</font>"<font face='Lucida Console'>)</font>; 
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>    <font color='#009900'>// member function definitions
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    ~<b><a name='queue_kernel_2'></a>queue_kernel_2</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>COMPILE_TIME_ASSERT</font><font face='Lucida Console'>(</font><font color='#979000'>0</font> <font color='#5555FF'>&lt;</font> block_size <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> block_size <font color='#5555FF'>&lt;</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font><font face='Lucida Console'>(</font><font color='#979000'>2000000000</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>queue_size <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <font color='#BB00BB'>delete_nodes</font><font face='Lucida Console'>(</font>out,in<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='clear'></a>clear</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>queue_size <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#BB00BB'>delete_nodes</font><font face='Lucida Console'>(</font>out,in<font face='Lucida Console'>)</font>;
            queue_size <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <b>}</b>

        <font color='#009900'>// put the enumerator at the start
</font>        <font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='enqueue'></a>enqueue</b> <font face='Lucida Console'>(</font>
        T<font color='#5555FF'>&amp;</font> item
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>queue_size <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
        <b>{</b>
            out <font color='#5555FF'>=</font> in <font color='#5555FF'>=</font> pool.<font color='#BB00BB'>allocate</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            in_pos <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            out_pos <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <b>}</b>
        <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>in_pos <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> block_size<font face='Lucida Console'>)</font>
        <b>{</b>            
            in<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>next <font color='#5555FF'>=</font> pool.<font color='#BB00BB'>allocate</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            in_pos <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            in <font color='#5555FF'>=</font> in<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>next;
        <b>}</b>

        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>item,in<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>item[in_pos]<font face='Lucida Console'>)</font>;
        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>in_pos;

        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>queue_size;

        <font color='#009900'>// put the enumerator at the start
</font>        <font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='dequeue'></a>dequeue</b> <font face='Lucida Console'>(</font>
        T<font color='#5555FF'>&amp;</font> item
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#009900'>// swap out into item
</font>        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>item,out<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>item[out_pos]<font face='Lucida Console'>)</font>;
        
        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>out_pos;
        <font color='#5555FF'>-</font><font color='#5555FF'>-</font>queue_size;

        <font color='#009900'>// if this was the last element in this node then remove this node
</font>        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>out_pos <font color='#5555FF'>=</font><font color='#5555FF'>=</font> block_size<font face='Lucida Console'>)</font>
        <b>{</b>
            out_pos <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            node<font color='#5555FF'>*</font> temp <font color='#5555FF'>=</font> out;
            out <font color='#5555FF'>=</font> out<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>next;
            pool.<font color='#BB00BB'>deallocate</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font>;
        <b>}</b>
        <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>queue_size <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
        <b>{</b>
            pool.<font color='#BB00BB'>deallocate</font><font face='Lucida Console'>(</font>out<font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#009900'>// put the enumerator at the start
</font>        <font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='cat'></a>cat</b> <font face='Lucida Console'>(</font>
        queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> item
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>queue_size <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
        <b>{</b>
            T temp;
            <font color='#BB00BB'>assign_zero_if_built_in_scalar_type</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>item.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <b>{</b>
                item.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font>;
                <font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            in <font color='#5555FF'>=</font> item.in;
            out <font color='#5555FF'>=</font> item.out;
            out_pos <font color='#5555FF'>=</font> item.out_pos;
            in_pos <font color='#5555FF'>=</font> item.in_pos;

            queue_size <font color='#5555FF'>=</font> item.queue_size;
            item.queue_size <font color='#5555FF'>=</font> <font color='#979000'>0</font>;

            <font color='#009900'>// put the enumerator at the start
</font>            <font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>       
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    T<font color='#5555FF'>&amp;</font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='current'></a>current</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> out<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>item[out_pos];
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='current'></a>current</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>return</font> out<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>item[out_pos];
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='swap'></a>swap</b> <font face='Lucida Console'>(</font>
        queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> item
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>in,item.in<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>out,item.out<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>queue_size,item.queue_size<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>in_pos,item.in_pos<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>out_pos,item.out_pos<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>current_element,item.current_element<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>current_element_pos,item.current_element_pos<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>exchange</font><font face='Lucida Console'>(</font>at_start_,item.at_start_<font face='Lucida Console'>)</font>;        
        pool.<font color='#BB00BB'>swap</font><font face='Lucida Console'>(</font>item.pool<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>    <font color='#009900'>// enumerable function definitions
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='size'></a>size</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>return</font> queue_size;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>bool</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='at_start'></a>at_start</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>return</font> at_start_;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='reset'></a>reset</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        at_start_ <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
        current_element <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>bool</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='current_element_valid'></a>current_element_valid</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>return</font> <font face='Lucida Console'>(</font>current_element <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'>const</font> T<font color='#5555FF'>&amp;</font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='element'></a>element</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>return</font> current_element<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>item[current_element_pos];
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    T<font color='#5555FF'>&amp;</font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='element'></a>element</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> current_element<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>item[current_element_pos];
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>bool</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='move_next'></a>move_next</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>at_start_<font face='Lucida Console'>)</font>
        <b>{</b>
            at_start_ <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
            <font color='#009900'>// if the queue is empty then there is nothing to do
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>queue_size <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>return</font> <font color='#979000'>false</font>;
            <b>}</b>
            <font color='#0000FF'>else</font>
            <b>{</b>
                current_element <font color='#5555FF'>=</font> out;
                current_element_pos <font color='#5555FF'>=</font> out_pos;
                <font color='#0000FF'>return</font> <font color='#979000'>true</font>;
            <b>}</b>
        <b>}</b>
        <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>current_element <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>return</font> <font color='#979000'>false</font>;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>current_element_pos;
            <font color='#009900'>// if we are at the last element then the enumeration has finished
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>current_element <font color='#5555FF'>=</font><font color='#5555FF'>=</font> in <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> current_element_pos <font color='#5555FF'>=</font><font color='#5555FF'>=</font> in_pos <font face='Lucida Console'>)</font>
            <b>{</b>
                current_element <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                <font color='#0000FF'>return</font> <font color='#979000'>false</font>;
            <b>}</b>
            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>current_element_pos <font color='#5555FF'>=</font><font color='#5555FF'>=</font> block_size<font face='Lucida Console'>)</font>
            <b>{</b>
                current_element_pos <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                current_element <font color='#5555FF'>=</font> current_element<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>next;               
            <b>}</b>           

            <font color='#0000FF'>return</font> <font color='#979000'>true</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>    <font color='#009900'>// remover function definitions
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='remove_any'></a>remove_any</b> <font face='Lucida Console'>(</font>
        T<font color='#5555FF'>&amp;</font> item
    <font face='Lucida Console'>)</font> 
    <b>{</b>
        <font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>item<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>    <font color='#009900'>// private member function definitions
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> T,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> block_size,
        <font color='#0000FF'>typename</font> mem_manager
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>void</u></font> queue_kernel_2<font color='#5555FF'>&lt;</font>T,block_size,mem_manager<font color='#5555FF'>&gt;</font>::
    <b><a name='delete_nodes'></a>delete_nodes</b> <font face='Lucida Console'>(</font>
        node<font color='#5555FF'>*</font> start,
        node<font color='#5555FF'>*</font> end
    <font face='Lucida Console'>)</font>
    <b>{</b>
        node<font color='#5555FF'>*</font> temp;
        <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>start <font color='#5555FF'>!</font><font color='#5555FF'>=</font> end<font face='Lucida Console'>)</font>
        <b>{</b>
            temp <font color='#5555FF'>=</font> start;
            start <font color='#5555FF'>=</font> start<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>next;
            pool.<font color='#BB00BB'>deallocate</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font>;
        <b>}</b>
        pool.<font color='#BB00BB'>deallocate</font><font face='Lucida Console'>(</font>start<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_QUEUE_KERNEl_2_
</font>

</pre></body></html>